import cgi
import datetime
import wsgiref.handlers
import os
import MyClassService
import Report
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
  name=db.StringProperty()

class MainPage(webapp.RequestHandler):
    def get(self):
      template_values = {

      }
      path = os.path.join(os.path.dirname(__file__), 'index.html')
      self.response.out.write(template.render(path, template_values))

class AddItem(webapp.RequestHandler):
    def get(self):
        greeting = Greeting()
        greeting.content = "content"
        greeting.name="zbo"
        greeting.put()
        self.response.out.write('add success')
        self.response.out.write('<br/>')
        self.response.out.write("""<a href="/">back</a>""")

class ClearItem(webapp.RequestHandler):
    def get(self):
        greetingsAll=Greeting.all()
        greetings = greetingsAll.fetch(3)
        for item in greetings:
          item.delete()

        self.response.out.write('clear success')
        self.response.out.write('<br/>')
        self.response.out.write("""<a href="/">back</a>""")
class DataReport (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_datareport.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class DataReport_Exam (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_datareport_exam.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class AddRecord(webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_addrecord.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class MngRecord (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_mngrecord.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class MngStudent (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_mngstudent.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class AddStudent (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_addstudent.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class AddExam (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_addexam.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class MngExam (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_mngexam.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))

class MngClass (webapp.RequestHandler):
    def get(self):
        directory = os.path.dirname(__file__)
        path = os.path.join(directory,'page_mngclass.html')
        template_values = {
        }
        self.response.out.write(template.render(path, template_values))
##service part

class AddExam_Action_Add (webapp.RequestHandler):
    def post(self):
        courseNameInput=cgi.escape(self.request.get('courseName'))
        examNameInput=cgi.escape(self.request.get('examName'))
        self.response.out.write(MyClassService.Service_AddExam(courseNameInput,examNameInput));

class AddUser_Action_AddParent (webapp.RequestHandler):
    def post(self):
        studentNameInput=cgi.escape(self.request.get('studentName'))
        studentNumberInput=cgi.escape(self.request.get('studentNumber'))
        parentNameInput=cgi.escape(self.request.get('parentName'))
        parentMailInput=cgi.escape(self.request.get('parentMail'))
        self.response.out.write(MyClassService.Service_AddParent(parentMailInput,studentNumberInput,studentNameInput,parentNameInput));

class AddUser_Action_AddStudent (webapp.RequestHandler):
    def post(self):
        studentNameInput=cgi.escape(self.request.get('studentName'))
        studentNumberInput=cgi.escape(self.request.get('studentNumber'))
        studentMailInput=cgi.escape(self.request.get('studentMail'))
        studentSexInput=cgi.escape(self.request.get('studentSex'))
        studentPwdInput=cgi.escape(self.request.get('pwd'))
        self.response.out.write(MyClassService.Service_AddStudent(studentNameInput,studentNumberInput,studentMailInput,studentSexInput,studentPwdInput));

class MngExam_Action_Search (webapp.RequestHandler):
    def post(self):
        courseNameInput=cgi.escape(self.request.get('courseName'))
        examNameInput=cgi.escape(self.request.get('examName'))
        exams=MyClassService.Service_SearchExam(courseNameInput,examNameInput);
        self.response.out.write(exams)

class MngExam_Action_Delete (webapp.RequestHandler):
    def post(self):
        courseNameInput=cgi.escape(self.request.get('courseName'))
        examNameInput=cgi.escape(self.request.get('examName'))
        exams=MyClassService.Service_DeleteExam(courseNameInput,examNameInput);
        self.response.out.write(exams)

class MngClass_Action_Add (webapp.RequestHandler):
    def post(self):
        classNameInput=cgi.escape(self.request.get('className'))
        self.response.out.write(MyClassService.Service_AddClass(classNameInput));

class MngClass_Action_Search (webapp.RequestHandler):
    def post(self):
        classNameInput=cgi.escape(self.request.get('className'))
        self.response.out.write(MyClassService.Service_SearchClass(classNameInput));

class MngClass_Action_Delete (webapp.RequestHandler):
    def post(self):
        classNameInput=cgi.escape(self.request.get('className'))
        self.response.out.write(MyClassService.Service_DeleteClass(classNameInput));

class MngClass_Action_Active (webapp.RequestHandler):
    def post(self):
        classNameInput=cgi.escape(self.request.get('className'))
        self.response.out.write(MyClassService.Service_ActiveClass(classNameInput));

class MngUser_Action_SearchStudent (webapp.RequestHandler):
    def post(self):
        choiseInput=cgi.escape(self.request.get('choise'))
        self.response.out.write(MyClassService.Service_SearchStudent(choiseInput));

class MngUser_Action_DeleteStudent (webapp.RequestHandler):
    def post(self):
        studentNumberInput=cgi.escape(self.request.get('studentNumber'))
        self.response.out.write(MyClassService.Service_DeleteStudent(studentNumberInput));

class AddRecord_Action_SearchStudentByNumber (webapp.RequestHandler):
    def post(self):
        studentNumberInput=cgi.escape(self.request.get('studentNumber'))
        self.response.out.write(MyClassService.Service_SearchStudentByNumber(studentNumberInput));

class AddRecord_Action_SearchExam (webapp.RequestHandler):
    def post(self):
        examNameInput=cgi.escape(self.request.get('examName'))
        courseNameInput=cgi.escape(self.request.get('courseName'))
        self.response.out.write(MyClassService.Service_SearchExamByName(examNameInput,courseNameInput));

class AddRecord_Action_AddExamRecordList (webapp.RequestHandler):
    def post(self):
        examRecordJsonInput=cgi.escape(self.request.get('record_json'))
        self.response.out.write(MyClassService.Service_AddExamRecordList(examRecordJsonInput));

class Common_Action_GetAllStudent (webapp.RequestHandler):
    def post(self):
        self.response.out.write(MyClassService.Service_GetAllStudents());

class MngRecord_Action_SearchExamByCourse(webapp.RequestHandler):
    def post(self):
        courseName=cgi.escape(self.request.get('courseName'))
        self.response.out.write(MyClassService.Service_SearchExamByCourse(courseName));

class Mngrecord_Action_SearchRecordByExamAndCourse(webapp.RequestHandler):
    def post(self):
        examName=cgi.escape(self.request.get('examName'))
        courseName=cgi.escape(self.request.get('courseName'))
        self.response.out.write(MyClassService.Service_SearchRecordByExamAndCourse(examName,courseName));

class Mnguser_Action_SearchStudentByNumber(webapp.RequestHandler):
    def post(self):
        studentNumber=cgi.escape(self.request.get('studentNumber'))
        self.response.out.write(MyClassService.Service_SearchStudentByNumber(studentNumber));

class Mnguser_Action_ResetPasswordByStudentNymber(webapp.RequestHandler):
    def post(self):
        studentNumber=cgi.escape(self.request.get('studentNumber'))
        self.response.out.write(MyClassService.Service_ResetPasswordByStudentNymber(studentNumber));

class Mngrecord_Action_UpdateSingleRecord(webapp.RequestHandler):
    def post(self):
        studentNumber=cgi.escape(self.request.get('studentNumber'))
        examName=cgi.escape(self.request.get('examName'))
        courseName=cgi.escape(self.request.get('courseName'))
        newValue=cgi.escape(self.request.get('newValue'))
        self.response.out.write(MyClassService.Service_UpdateSingleRecord(studentNumber,examName,courseName,newValue));

class Login(webapp.RequestHandler):
    def post(self):
        userName=cgi.escape(self.request.get('userName'))
        passWord=cgi.escape(self.request.get('passWord'))
        self.response.out.write(MyClassService.Login(userName,passWord));




application = webapp.WSGIApplication([
  ('/', MainPage),
  ('/add',AddItem),
  ('/clear',ClearItem),
  ('/addrecord',AddRecord),
  ('/mngrecord',MngRecord),
  ('/mngstudent',MngStudent),
  ('/addstudent',AddStudent),
  ('/addexam',AddExam),
  ('/mngexam',MngExam),
  ('/mngclass',MngClass),
  ('/datareport',DataReport),
  ('/datareport_exam',DataReport_Exam),
  ('/adduser_action_addstudent',AddUser_Action_AddStudent),
  ('/adduser_action_addparent',AddUser_Action_AddParent),
  ('/addexam_action_add',AddExam_Action_Add),
  ('/mngexam_action_search',MngExam_Action_Search),
  ('/mngexam_action_delete',MngExam_Action_Delete),
  ('/mngclass_action_add',MngClass_Action_Add),
  ('/mngclass_action_search',MngClass_Action_Search),
  ('/mngclass_action_delete',MngClass_Action_Delete),
  ('/mngclass_action_active',MngClass_Action_Active),
  ('/mnguser_action_searchStudent',MngUser_Action_SearchStudent),
  ('/mnguser_action_deleteStudent',MngUser_Action_DeleteStudent),
  ('/addrecord_action_searchStudent',AddRecord_Action_SearchStudentByNumber),
  ('/addrecord_action_searchExam',AddRecord_Action_SearchExam),
  ('/common_action_getAllStudent',Common_Action_GetAllStudent),
  ('/addrecord_action_addExamRecordList',AddRecord_Action_AddExamRecordList),
  ('/mngrecord_action_searchExamByCourse',MngRecord_Action_SearchExamByCourse),
  ('/mngrecord_action_searchRecordByExamAndCourse',Mngrecord_Action_SearchRecordByExamAndCourse),
  ('/mnguser_action_searchStudentByNumber',Mnguser_Action_SearchStudentByNumber),
  ('/mnguser_action_resetPasswordByStudentNymber',Mnguser_Action_ResetPasswordByStudentNymber),
  ('/mngrecord_action_updateSingleRecord',Mngrecord_Action_UpdateSingleRecord),
  ('/report_gen_fromStudentNumberANDCourse',Report.Report_Student_CourseReport),
  ('/report_gen_classPie',Report.Report_Gen_ClassPie),
  ('/login',Login),

], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()
